package dk.flashnotes.fff.helpers {
	import dk.flashnotes.fff.helpers.Helper;	

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.SharedObject;	

	/**
	 * @author jk
	 */
	public class HelperBaseUI extends Helper {

		public static const nBACK_MARGIN : Number = 7;
		public static const nTOP_BAR : Number = 0;

		private var _sprBack : Sprite;
		private var _sSOStoreID : String;

		private var pInitPosition : Point=new Point(10, 10 + nTOP_BAR);

		public function HelperBaseUI(sSOStoreID : String) {
			_sSOStoreID = sSOStoreID;
			
			createBack();
			setPostion();
			setupAlphaBack();
			setupDragable();
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// reset position
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function resetPosition() : void {
			var oSOPosition : SharedObject = SharedObject.getLocal(_sSOStoreID);
			oSOPosition.clear();
			setPostion();
		}

		private function setupDragable() : void {
			_sprBack.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, false, 0, true);
		}

		private function onMouseDown(e : Event) : void {
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			startDrag();
		}

		private function onMouseUp(e : Event) : void {
			stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			savePosition();
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// alpha back
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function setupAlphaBack() : void {
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver, false, 0, true);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut, false, 0, true);
			onMouseOut(null);
		}

		private function onMouseOut(e : Event) : void {
			alpha = 0.6;
		}

		private function onMouseOver(e : Event) : void {
			alpha = 0.8;
		}

		
		/*******************
		 * create do
		 ********************/

		public function updateBack() : void {
			drawBack();
		}

		private function createBack() : void {
			
			_sprBack = new Sprite();
			addChildAt(_sprBack, 0);
			
			drawBack();
		}

		private function drawBack() : void {
			_sprBack.graphics.clear();
			_sprBack.graphics.beginFill(0x333333);
			_sprBack.graphics.drawRoundRect(-nBACK_MARGIN, -nBACK_MARGIN - nTOP_BAR, width + 2 * nBACK_MARGIN, nTOP_BAR + height + 2 * nBACK_MARGIN, 5, 5);
		}

		/*******************
		 * position control
		 ********************/

		private function setPostion() : void {
			var pPos : Point = getLastPosition();
			if (!pPos) {
				pPos = pInitPosition;
			}
			
			x = pPos.x;
			y = pPos.y;
		}

		private function getLastPosition() : Point {
			
			var oSOPosition : SharedObject = SharedObject.getLocal(_sSOStoreID);
			
			if (oSOPosition) {
				
				var x : Number = Number(oSOPosition.data.x);
				var y : Number = Number(oSOPosition.data.y);
				
				if (!isNaN(x) && !isNaN(y)) {
					return new Point(x, y);
				}
			}
			return null;
		}

		private function savePosition() : void {
			var oSOPosition : SharedObject = SharedObject.getLocal(_sSOStoreID);
			oSOPosition.data.x = x;
			oSOPosition.data.y = y;
		}
	}
}

